package com.fernfx.xingtan.main.ui;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.CompoundButton;
import android.widget.RadioGroup;

import com.fernfx.xingtan.Constant;
import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseActivity;
import com.fernfx.xingtan.common.entity.user.MemberEntity;
import com.fernfx.xingtan.common.entity.user.UserInfoEntity;
import com.fernfx.xingtan.main.contract.MainContract;
import com.fernfx.xingtan.main.presenter.MainPresenter;
import com.fernfx.xingtan.user.AccountManager;
import com.fernfx.xingtan.utils.FragmentFactoryUtil;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnCheckedChanged;
import io.rong.imkit.RongIM;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.UserInfo;

import static io.rong.imlib.statistics.UserData.name;

/**
 * 首页
 */
public class MainActivity extends BaseActivity implements MainContract.View {

    @BindView(R.id.bottom_tab_rgp)
    RadioGroup buttomTabRgp;

    private MainContract.Presenter presenter = new MainPresenter();

    public static void start(Context context) {
//        Intent intent = new Intent(context,MainActivity.class);
//        context.startActivity(intent);
        Map<String, Boolean> map = new HashMap<>();
        map.put(Conversation.ConversationType.PRIVATE.getName(), false); // 会话列表需要显示私聊会话, 第二个参数 true 代表私聊会话需要聚合显示
        map.put(Conversation.ConversationType.GROUP.getName(), false);  // 会话列表需要显示群组会话, 第二个参数 false 代表群组会话不需要聚合显示
        RongIM.getInstance().startConversationList(context, map);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        //初始化首页fragment
        presenter.init(this);
        presenter.loadSelectFragment(FragmentFactoryUtil.NAV_CHAT);
    }

    @Override
    protected void initData() {
        RongIM.setUserInfoProvider(new RongIM.UserInfoProvider() {
            @Override
            public UserInfo getUserInfo(String s) {//用户信息提供者的S会改变，所以从服务器拿，参考微商猎手
                final UserInfoEntity userInfoEntity = AccountManager.getUserInfo().getObj();

                UserInfo userInfo = new UserInfo("xingtan-"+userInfoEntity.getUserId(),
                        userInfoEntity.getNickname(), Uri.parse(userInfoEntity.getHeadImg()));
                return userInfo;
            }
        }, true);

    }

    @OnCheckedChanged({R.id.chat_rbn, R.id.contacts_rbn,R.id.my_rbn})
    public void onRadioButtonCheckChanged(CompoundButton button, boolean checked) {
        if (!checked) {
            return;
        }
        switch (button.getId()) {
            case R.id.chat_rbn:
                presenter.loadSelectFragment(FragmentFactoryUtil.NAV_CHAT);
                break;
            case R.id.contacts_rbn:
                presenter.loadSelectFragment(FragmentFactoryUtil.NAV_HISTROY);
                break;
            case R.id.my_rbn:
                presenter.loadSelectFragment(FragmentFactoryUtil.NAV_MY);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RongIM.getInstance().disconnect();
    }

    @Override
    public BaseActivity getActivity() {
        return this;
    }
}
